package com.syx.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.syx.entity.Detail;
import com.syx.entity.Logs;
import com.syx.entity.User;
import com.syx.service.LogService;
import com.syx.util.FileUpload2;
import com.syx.util.SnowflakeIdUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LogController {
    @Resource
    private LogService service;
    private String oid="";

    //参照图示实现登录，
    @RequestMapping("login.do")
    public String login(User user, HttpSession session){
        User users=service.login(user);
        if (users!=null){
            //登录成功后，将雇员号、雇员姓名、部门、岗位等都存储起来（3分）
            session.setAttribute("user",users);
            return "redirect:list.do";
        }else{
            return "redirect:index.jsp";
        }
    }

    @RequestMapping("list.do")
    public String list(Model model,
                       String oid,
                       String otimeMin,
                       String otimeMax,
                       String rem,
                       String numsMin,
                       String numsMax,
                       String sumsMin,
                       String sumsMax,
                       @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
        PageHelper.startPage(pageNum,3);
        Map map = new HashMap();



        map.put("oid",oid);//(5)外协单号查询（2分）
        map.put("otimeMin",otimeMin); //(6)外协日期查询（2分）
        map.put("otimeMax",otimeMax);
        map.put("rem",rem);//(7)备注模糊查询（2分）
        map.put("numsMin",numsMin);
        map.put("numsMax",numsMax);
        map.put("sumsMin",sumsMin);
        map.put("sumsMax",sumsMax);
        List list = service.list(map);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("map",map);
        return "list";
    }

    @RequestMapping("addTo.do")
    public String addTo(Model model){
        return "add";
    }

    @RequestMapping("SnowId.do")
    @ResponseBody
    public Object SnowId(){
        if (oid.length()==0 ||  service.getSnowId(oid)>0){
            long l = new SnowflakeIdUtils(1, 1).nextId();
            oid=l+"";
            System.out.println("==================="+l);
        }
        return  oid;
    }


    //(3)明细页面实现新增 （2分）  //(3)明细页面实现新增 （3分）
    @RequestMapping("addDetail.do")
    public Object addDetail(Detail detail,Model model){
        if (detail.getOid()!=null||service.getSnowId(oid)>0){
            int i = service.getSnowId(detail.getOid());
            //(6)点击"提交"，实现"外协单新增"完整实现;外协单和外协明细都要插入，使用事务综合业务操作原子性、数据一致性;打开数据库表，印证各表数据正确实现。 （2分）
            if (i==0&&detail.getOid().length()>0){
                oid=detail.getOid();
                service.addLogsOid(oid);
            }
            service.addDetail(detail);
            model.addAttribute("oid",detail.getOid());
        }else{
            model.addAttribute("oid",oid);
        }
        //(7)跳转到“外协统计”页面，按照图示要求显示页面。 （2分）
        return "redirect:addTo.do";
    }

    @RequestMapping("detail.do")
    @ResponseBody
    public Object detail(String oid){
        Map map = service.detail(oid);
        return map;
    }

    //(2)附件上传可以更新（2分）
    @RequestMapping("plupload.do")
    @ResponseBody
    public Object plupload(MultipartFile file, HttpServletRequest request){
        String plupload = FileUpload2.plupload(file, request);
        System.out.println("======================================"+plupload);
        return plupload;
    }

    @RequestMapping("addUpd.do")
    @ResponseBody
    public Object addUpd(String path, Logs logs){
        logs.setPic(path);
        int i =service.addUpd(logs);
        return i;
    }

    @RequestMapping("del.do")
    @ResponseBody
    public Object del(String oid){
        int i = service.del(oid);
        return i;
    }

    @RequestMapping("updTo.do")
    public String updTo(Model model){
        return "upd";
    }

    @RequestMapping("hx.do")
    @ResponseBody
    public Object hx(String oid){
        //(1)完全正确回显整条记录 （2分）
        List list = service.hx(oid);
        return list;
    }

}
